<?php

namespace App\Http\Controllers\Admin\shop;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreProduct;
use App\Http\Requests\UpdateProduct;
use App\Models\Shop\Brand;
use App\Models\Shop\Category;
use App\Models\Shop\Product;
use App\Models\Shop\ProductGallery;
use DB;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function __construct()
    {
        view()->share([
            'categories' => Category::with('children')->where('parent_id', 0)->orderBy('sort_order', 'desc')->get(),
            'brands' => Brand::OrderBy('sort_order', 'desc')->get(),
            'filter_categories' => Category::filter_categories(),

            '_shop' => 'am-in',
            '_product' => 'am-active',
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $products = Product::all_products($request);
        $datas = array('is_top', 'is_onsale', 'is_recommend', 'is_hot', 'is_new');
        return view('admin.shop.product.index', compact('products', 'datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.product.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreProduct $request)
    {
        $product = Product::create($request->all());

        //商品所属栏目
        $product->categories()->sync($request->category_id);
        //相册
        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $product->product_galleries()->create(['img' => $img]);
            }
        }
        return redirect(route('shop.product.index'))->with('success', '新增商品成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product = Product::with('categories', 'product_galleries')->find($id);
        //当前商品对应的分类id
        $product_cate = $product->categories->pluck('id');
        return view('admin.shop.product.edit', compact('product', 'product_cate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateProduct $request, $id)
    {
        $product = Product::find($id);
        $product->update($request->all());

        $product->categories()->sync($request->category_id);

        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $product->product_galleries()->create(['img' => $img]);
            }
        }
        return redirect(route('shop.product.index'))->with('success', '编辑商品成功！');
    }


    /**
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $product = Product::find($request->id);
        $attr = $request->attr;
        $product->$attr = !$product->$attr;
        $product->save();
    }

    /**
     * 改变库存
     */
    public function stock(Request $request)
    {
        $product = Product::find($request->id);
        $product->stock = $request->stock;
        $product->save();
    }

    /**
     * 改变价格
     */
    public function price(Request $request)
    {
        $product = Product::find($request->id);
        $product->price = $request->price;
        $product->save();
    }

    /**
     * Ajax删除相册图片
     * @param Request $request
     * @return array
     */
    public function destroy_gallery(Request $request)
    {
        ProductGallery::destroy($request->gallery_id);
    }

    /**
     * 商品回收---ajax单个回收
     */
    public function recycle_one(Request $request)
    {
        Product::destroy($request->id);
    }

    /**
     * 商品回收---ajax多个回收
     */
    public function recycle_all(Request $request)
    {
        Product::destroy($request->check_id);
    }


    /**
     * 商品回收---首页
     */
    public function recycle()
    {
        view()->share([
            '_shop' => 'am-in',
            '_product' => '',
            '_recycle' => 'am-active',
        ]);

        $products = Product::with('categories', 'brand')->orderBy('sort_order')->onlyTrashed()->paginate(config('admin.page_size'));
        $datas = array('is_onsale', 'is_top', 'is_recommend', 'is_hot', '');
        return view('admin.shop.product.recycle', compact('products', 'datas'));
    }

    /**
     * Remove the specified resource from storage.
     * 单个删除
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //withTrashed()可以不写
        Product::withTrashed()->where('id', $id)->forceDelete();
        //单个删除要用where
        DB::table('category_product')->where('product_id', $id)->delete();
        DB::table('product_galleries')->where('product_id', $id)->delete();

        //适用于没有软删除中
//        Product::destroy($id);
//        if (is_array($id)) {
//            DB::table('category_product')->whereIn('product_id', $id)->delete();
//            DB::table('product_galleries')->whereIn('product_id', $id)->delete();
//        } else {
//            DB::table('category_product')->where('product_id', $id)->delete();
//            DB::table('product_galleries')->where('product_id', $id)->delete();
//        }
    }

    /**
     * 多个彻底删除
     */
    public function destroy_all(Request $request)
    {
        //多个删除要用whereIn
        Product::withTrashed()->whereIn('id', $request->check_id)->forceDelete();
        DB::table('category_product')->whereIn('product_id', $request->check_id)->delete();
        DB::table('product_galleries')->whereIn('product_id', $request->check_id)->delete();
    }

    /**
     * 单个还原
     */
    public function reply_one(Request $request)
    {
        Product::withTrashed()->where('id', $request->id)->restore();
    }

    /**
     * 多个还原
     */
    public function reply_all(Request $request)
    {
        //第一种方法
//        Product::withTrashed()->whereIn('id', $request->check_id)->restore();

        //第二种方法
        foreach ($request->check_id as $id) {
            Product::where('id', $id)->restore();
        }
    }

}
